package com.itheima.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.pojo.*;
import com.itheima.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin
@RequestMapping("/sort")
public class BlogSortController {

    //1.GET /sort/getArticleByMonth (monthDate 2020年03月)通过月份获取文章
    //{"code":"success","data":[{"title":"测试博客","summary":"测试博客2","tagUid":"5c4c541e600ff422ccb371ee788f59d6","blogSortUid":"1ec7047fe5c672d7c756bf79405cd04b","clickCount":2.0,"collectCount":0.0,"fileUid":"3cf2c985ccceb5e94155d57cda447c36","adminUid":"1f01cd1d2f474743b241d74008b12333","isPublish":"1","isOriginal":"1","author":"陌溪","articlesPart":"蘑菇博客","level":2.0,"sort":0.0,"startComment":"1","tagList":[null],"blogSort":{"sortName":"测试1","content":"测试1","clickCount":1.0,"sort":1.0,"uid":"1ec7047fe5c672d7c756bf79405cd04b","status":1.0,"createTime":"2020-08-04 07:43:29","updateTime":"2020-08-07 10:08:39"},"uid":"e620f3578d21cc57aee33a80d67b26b9","status":1.0,"createTime":"2020-03-31 22:40:35","updateTime":"2020-06-04 09:20:00"}]}

    @Autowired
    private BlogService blogService;

    @GetMapping("/getArticleByMonth")
    public Map getArticleByMonth(String monthDate) throws Exception {

        PageInfo<Blog> page=blogService.getArticleByMonth(monthDate);

        List<Blog> blogList = page.getList();

        //封装查询到的数据
        List<Map<String,Object>> list=new ArrayList<>();

        for (Blog blog : blogList) {

            Map<String, Object> map1 = new HashMap<>();

            map1.put("title",blog.getTitle());
            map1.put("summary",blog.getSummary());
            map1.put("tagUid",blog.getTagUid());
            map1.put("blogSortUid",blog.getBlogSortUid());
            map1.put("clickCount",blog.getClickCount());
            map1.put("collectCount",blog.getCollectCount());
            map1.put("fileUid",blog.getFileUid());
            map1.put("adminUid",blog.getAdminUid());

            map1.put("isPublish",blog.getIsPublish());
            map1.put("isOriginal",blog.getIsOriginal());
            map1.put("author",blog.getAuthor());
            map1.put("articlesPart",blog.getArticlesPart());

            map1.put("level",blog.getLevel());
            map1.put("sort",blog.getSort());
            map1.put("startComment",blog.getStartComment());

             //封装博客标签数据
            List<Tag> tagList2 = blog.getTagList();
            List<Map<String,Object>> list3=new ArrayList<>();
            for (Tag tag : tagList2) {
                Map<String, Object> map3 = new HashMap<>();
                // {"content":"测试标签","clickCount":18,"sort":18,"uid":"5c4c541e600ff422ccb371ee788f59d6","status":1,"createTime":"2020-08-05 16:51:11","updateTime":"2020-08-05 16:51:11"}]
                map3.put("content",tag.getContent());
                map3.put("clickCount",tag.getSort());
                map3.put("sort",tag.getContent());
                map3.put("uid",tag.getUid());
                map3.put("status",tag.getStatus());
                map3.put("createTime", DateUtils.parseDate2String(tag.getCreateTime()));
                map3.put("updateTime",DateUtils.parseDate2String(tag.getUpdateTime()));
                list3.add(map3);
            }
            map1.put("tagList",list3);

            //封装博客分类数据
            BlogSort blogSort = blog.getBlogSort();
            Map<String, Object> map4 = new HashMap<>();
            //{"sortName":"测试3","content":"测试3","clickCount":2,"sort":10,"uid":"718c76d4a84e408da23e04949a75e7e8","status":1,"createTime":"2020-03-31 20:08:00","updateTime":"2020-08-05 16:39:56"}
            map4.put("sortName",blogSort.getSortName());
            map4.put("content",blogSort.getContent());
            map4.put("clickCount",blogSort.getClickCount());
            map4.put("sort",blogSort.getSort());
            map4.put("uid",blogSort.getUid());
            map4.put("status",blogSort.getStatus());
            map4.put("createTime",DateUtils.parseDate2String(blogSort.getCreateTime()));
            map4.put("updateTime",DateUtils.parseDate2String(blogSort.getUpdateTime()));
            map1.put("blogSort",map4);

            //"uid":"07d5a3bfdbb1267904c6bcf2837761cd","status":1,"createTime":"2020-04-24 08:47:54","updateTime":"2020-08-05 16:32:21"
            map1.put("uid",blog.getUid());
            map1.put("status",blog.getStatus());
            map1.put("createTime",DateUtils.parseDate2String(blog.getCreateTime()));
            map1.put("updateTime",DateUtils.parseDate2String(blog.getUpdateTime()));
            list.add(map1);
        }

        Map<String, Object> result = new HashMap<>();
        //返回结果
        result.put("code", "success");
        result.put("data", list);
        return result;
    }


     //2.GET /sort/getSortList 归档
    //{"code":"success","data":["2020年03月","2020年04月","2020年05月","2020年06月","2020年08月"]}
    @RequestMapping("/getSortList")
    public Result getSortList(){

        try {
            List<String>list= blogService.getSortList();
            new Result(StatusCode.OK,list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(StatusCode.ERROR,null);
    }
}
